<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增住院登记')" />
    <th:block th:include="include :: datetimepicker-css" />
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-registration-add">
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">患者：</label>
                <div class="col-sm-8">
                    <select name="patientId" id="patientId" class="form-control" required>
                        <!-- 分类选项将通过 JavaScript 动态填充 -->
                        <option th:each="patient:${tPatients}" th:value="${patient.id}"
                                th:text="${patient.name}"></option>
                    </select>
                </div>
            </div>
        </div>
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">入院日期：</label>
                <div class="col-sm-8">
                    <div class="input-group date">
                        <input name="admissionDate" class="form-control" placeholder="yyyy-MM-dd" type="text" required>
                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">预计出院日期：</label>
                <div class="col-sm-8">
                    <div class="input-group date">
                        <input name="estimatedDischargeDate" class="form-control" placeholder="yyyy-MM-dd" type="text" required>
                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">主治医生：</label>
                <div class="col-sm-8">
                    <select name="doctorId" id="doctorId" class="form-control" required>
                        <!-- 分类选项将通过 JavaScript 动态填充 -->
                        <option th:each="doctor:${tDoctors}" th:value="${doctor.id}"
                                th:text="${doctor.name}"></option>
                    </select>
                </div>
            </div>
        </div>
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">责任护士：</label>
                <div class="col-sm-8">
                    <select name="nurseId" id="nurseId" class="form-control" required>
                        <!-- 分类选项将通过 JavaScript 动态填充 -->
                        <option th:each="nurse:${tNurses}" th:value="${nurse.id}"
                                th:text="${nurse.name}"></option>
                    </select>
                </div>
            </div>
        </div>
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">床位：</label>
                <div class="col-sm-8">
                    <select name="bedId" id="bedId" class="form-control" required>
                        <!-- 分类选项将通过 JavaScript 动态填充 -->
                        <option th:each="bed:${tBeds}" th:value="${bed.id}"
                                th:text="${bed.bedNumber}"></option>
                    </select>
                </div>
            </div>
        </div>
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">所属病房：</label>
                <div class="col-sm-8">
                    <select name="wardId" id="wardId" class="form-control" required>
                        <!-- 分类选项将通过 JavaScript 动态填充 -->
                        <option th:each="ward:${tWards}" th:value="${ward.id}"
                                th:text="${ward.wardNumber}"></option>
                    </select>
                </div>
            </div>
        </div>
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">总费用：</label>
                <div class="col-sm-8">
                    <input name="totalCost" class="form-control" type="text" readonly>
                </div>
            </div>
        </div>
    </form>
</div>
<th:block th:include="include :: footer" />
<th:block th:include="include :: datetimepicker-js" />
<script th:inline="javascript">
    var prefix = ctx + "system/registration"
    $("#form-registration-add").validate({
        focusCleanup: true
    });

    function submitHandler() {
        if ($.validate.form()) {
            $.operate.save(prefix + "/add", $('#form-registration-add').serialize());
        }
    }

    $("input[name='admissionDate']").datetimepicker({
        format: "yyyy-mm-dd",
        minView: "month",
        autoclose: true
    });

    $("input[name='estimatedDischargeDate']").datetimepicker({
        format: "yyyy-mm-dd",
        minView: "month",
        autoclose: true
    });

    // 新增计算总费用的函数
    function calculateTotalCost() {
        var admissionDate = new Date($("input[name='admissionDate']").val());
        var estimatedDischargeDate = new Date($("input[name='estimatedDischargeDate']").val());
        var timeDifference = Math.ceil((estimatedDischargeDate - admissionDate) / (1000 * 60 * 60 * 24));
        var totalCost = timeDifference * 80;
        $("input[name='totalCost']").val(totalCost);
    }

    // 为出院日期输入框添加 change 事件监听
    $("input[name='estimatedDischargeDate']").change(calculateTotalCost);
    // 为入院日期输入框添加 change 事件监听
    $("input[name='admissionDate']").change(calculateTotalCost);
</script>
</body>
</html>